<template>
   <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
         <el-form-item label="统计日期" prop="summaryDate">
            <el-date-picker
               v-model="queryParams.summaryDate"
               type="date"
               placeholder="选择统计日期"
               style="width: 200px"
            />
         </el-form-item>
         <el-form-item label="门店" prop="storeId">
            <el-input
               v-model="queryParams.storeName"
               placeholder="请选择门店"
               clearable
               style="width: 200px"
               @keyup.enter="handleQuery"
            />
         </el-form-item>
         <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
         </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
         <el-col :span="1.5">
            <el-button
               type="warning"
               plain
               icon="Download"
               @click="handleExport"
               v-hasPermi="['data:online-summary:export']"
            >导出</el-button>
         </el-col>
         <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <!-- 统计卡片 -->
      <el-row :gutter="20" class="mb8">
         <el-col :span="6">
            <el-card shadow="hover">
               <div class="stat-card">
                  <div class="stat-label">在线设备数</div>
                  <div class="stat-value">{{ summaryData.onlineDeviceCount || 0 }}</div>
               </div>
            </el-card>
         </el-col>
         <el-col :span="6">
            <el-card shadow="hover">
               <div class="stat-card">
                  <div class="stat-label">离线设备数</div>
                  <div class="stat-value text-danger">{{ summaryData.offlineDeviceCount || 0 }}</div>
               </div>
            </el-card>
         </el-col>
         <el-col :span="6">
            <el-card shadow="hover">
               <div class="stat-card">
                  <div class="stat-label">今日识别次数</div>
                  <div class="stat-value text-primary">{{ summaryData.todayRecognitionCount || 0 }}</div>
               </div>
            </el-card>
         </el-col>
         <el-col :span="6">
            <el-card shadow="hover">
               <div class="stat-card">
                  <div class="stat-label">总识别次数</div>
                  <div class="stat-value text-success">{{ summaryData.totalRecognitionCount || 0 }}</div>
               </div>
            </el-card>
         </el-col>
      </el-row>

      <el-table v-loading="loading" :data="summaryList">
         <el-table-column type="index" label="序号" width="60" align="center" />
         <el-table-column label="门店名称" align="center" prop="storeName" :show-overflow-tooltip="true" />
         <el-table-column label="设备总数" align="center" prop="deviceTotal" />
         <el-table-column label="在线设备" align="center" prop="onlineDevice" />
         <el-table-column label="离线设备" align="center" prop="offlineDevice" />
         <el-table-column label="今日识别" align="center" prop="todayRecognition" />
         <el-table-column label="总识别数" align="center" prop="totalRecognition" />
         <el-table-column label="在线率" align="center" prop="onlineRate">
            <template #default="scope">
               <span :class="scope.row.onlineRate >= 90 ? 'text-success' : 'text-warning'">
                  {{ scope.row.onlineRate || 0 }}%
               </span>
            </template>
         </el-table-column>
         <el-table-column label="统计时间" align="center" prop="summaryTime" width="180">
            <template #default="scope">
               <span>{{ parseTime(scope.row.summaryTime) }}</span>
            </template>
         </el-table-column>
      </el-table>

      <pagination
         v-show="total > 0"
         :total="total"
         v-model:page="queryParams.pageNum"
         v-model:limit="queryParams.pageSize"
         @pagination="getList"
      />
   </div>
</template>

<script setup name="OnlineSummary">
import { listOnlineSummary, getSummaryData } from "@/api/data/online-summary";

const { proxy } = getCurrentInstance();

const summaryList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const summaryData = ref({
  onlineDeviceCount: 0,
  offlineDeviceCount: 0,
  todayRecognitionCount: 0,
  totalRecognitionCount: 0
});

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  summaryDate: undefined,
  storeId: undefined,
  storeName: undefined
});

/** 查询在线汇总列表 */
function getList() {
  loading.value = true;
  // TODO: 调用实际API
  // listOnlineSummary(queryParams.value).then(response => {
  //   summaryList.value = response.rows;
  //   total.value = response.total;
  //   loading.value = false;
  // });
  
  // 获取汇总数据
  // getSummaryData(queryParams.value).then(response => {
  //   summaryData.value = response.data;
  // });
  
  setTimeout(() => {
    summaryList.value = [];
    total.value = 0;
    summaryData.value = {
      onlineDeviceCount: 0,
      offlineDeviceCount: 0,
      todayRecognitionCount: 0,
      totalRecognitionCount: 0
    };
    loading.value = false;
    proxy.$modal.msgWarning("在线汇总功能开发中，请等待后端接口...");
  }, 500);
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download("data/online-summary/export", {
    ...queryParams.value
  }, `online-summary_${new Date().getTime()}.xlsx`);
}

getList();
</script>

<style scoped>
.stat-card {
  text-align: center;
}

.stat-label {
  font-size: 14px;
  color: #909399;
  margin-bottom: 10px;
}

.stat-value {
  font-size: 32px;
  font-weight: bold;
  color: #303133;
}

.text-primary {
  color: #409EFF;
}

.text-success {
  color: #67C23A;
}

.text-danger {
  color: #F56C6C;
}

.text-warning {
  color: #E6A23C;
}
</style>

